﻿using AutoMapper;
using MediatR;
using WMS_Goodsinfo.Api.Write.Application.Command.商品;
using WMS_Goodsinfo.Domain;
using WMS_Goodsinfo.ErrorCode;
using WMS_Goodsinfo.Infastructure;

namespace WMS_Goodsinfo.Api.Write.Application.Handler.商品
{
    public class CreateWarehouseHandler : IRequestHandler<CreateWarehouseCommand, ApiResult<int>>
    {
        private readonly IProductRepository<Warehouse> _warehouseRepo;
        private readonly IMapper _mapper;

        public CreateWarehouseHandler(IProductRepository<Warehouse> warehouseRepo, IMapper mapper)
        {
            _warehouseRepo = warehouseRepo;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加库存
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(CreateWarehouseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var model =_mapper.Map<Warehouse>(request);
            model.CreateTime= DateTime.Now;

            var res=_warehouseRepo.Add(model);
            if (res > 0)
            {
                result.Code = StatusEnum.OK;
                result.Data = res;
                result.Message = "添加成功";
            }
            else
            {
                result.Code = StatusEnum.Fail;
                result.Data = res;
                result.Message = "添加失败";
            }
            return Task.FromResult(result);
        }
    }
}
